/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'avoid_area_manager.dart';
import 'game_page.dart';

void main() {
  runApp(const MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late AvoidAreaManager areaManager;
  var avoidAreaState = AvoidAreaState();
  late ValueChanged<AvoidAreaState> listener;

  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    areaManager = AvoidAreaManager();
    listener = (value) {
      Future.microtask(() {
        setState(() {
          avoidAreaState = value;
        });
      });
    };
    areaManager.addListener(listener);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    areaManager.updateMediaQuery(context);
  }

  @override
  void dispose() {
    super.dispose();
    areaManager.removeListener(listener);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        Image.asset(
          'assets/main_bg.png',
          fit: BoxFit.cover,
          width: double.infinity,
          height: double.infinity,
        ),
        Padding(
          padding: avoidAreaState.padding,
          child: Stack(
            children: [
              Row(
                children: [
                  createSizedBox(CutoutPosition.topLeft,
                      widthOffset: getWidthOffset(true)),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Image.asset('assets/pause.png'),
                        Image.asset('assets/icon_list.png'),
                      ],
                    ),
                  )),
                  createSizedBox(CutoutPosition.topRight,
                      widthOffset: getWidthOffset(false)),
                ],
              ),
              Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset('assets/top.png'),
                    GestureDetector(
                      onTap: () async {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const GamePage()),
                        );
                      },
                      child: Transform.translate(
                        offset: const Offset(-15.0, 0.0),
                        child: Image.asset('assets/go_game.png'),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        )
      ],
    ));
  }

  Widget createSizedBox(CutoutPosition cutoutPosition,
      {double widthOffset = 0, double heightOffset = 0}) {
    return cutoutPosition == avoidAreaState.cutoutPosition
        ? SizedBox(
      width: avoidAreaState.rect.width + widthOffset,
      height: avoidAreaState.rect.height + heightOffset,
    )
        : const SizedBox(
      width: 0,
      height: 0,
    );
  }

  double getWidthOffset(bool isLeft) {
    if (isLeft) {
      return avoidAreaState.rect.left < 0 ? 0 : avoidAreaState.rect.left;
    }
    double offsetRight =
    (areaManager.getScreenWidth() - avoidAreaState.rect.right);
    if (offsetRight < 0) {
      offsetRight = 0;
    }
    return offsetRight;
  }
}
